package com.company.project.common.utils;

import com.google.gson.Gson;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
 * @author : GWFan
 * time : 2024/8/21 18:07
 * desc :
 **/
public class DoubleCallShowADemo {
    /**
     * @author : GWFan
     * time : 2024/8/21 18:18
     * desc : 外呼
     **/
    public static void callOut() {
        Map<String, Object> p = new HashMap<>();
        p.put("ts", System.currentTimeMillis() / 1000);
        p.put("type", "TYC");//固定传 TYC
        p.put("callerId", "员工编号（坐席编号）");//联系管理员开通账户，同时提供员工编号
        List<String> callerNoList = new ArrayList<>();
        callerNoList.add("183xxxx3005");
        p.put("callerNos", callerNoList);
        p.put("calledNo", "被叫号码");
        p.put("payload", "扩展字段");//话单推送时回传
        String sign = stringToMD5((new Gson().toJson(p) + "替换成自己的secret"));
                HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        headers.add("api_key", "替换成自己的 key");
        headers.add("sign", sign);
        HttpEntity<String> entity = new HttpEntity<>(new Gson().toJson(p), headers);
        ResponseEntity<String> resp = new RestTemplate(new
                SimpleClientHttpRequestFactory()).postForEntity("https://api" +
                ".51lianlian.cn/api/v1/binding", entity, String.class);
        System.out.println("resp-------->" + resp.getBody());
    }
    public static String stringToMD5(String plainText) {
        byte[] secretBytes = null;
        try {
            secretBytes = MessageDigest.getInstance("md5").digest(plainText.getBytes());
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        }
        String md5code = new BigInteger(1, secretBytes).toString(16);
        int length = 32 - md5code.length();
        for (int i = 0; i < length; i++) {
            md5code = "0" + md5code;
        }
        return md5code;
    }
}
